<?php

namespace App\Admin\Controllers;

use App\Models\Area;
use App\Models\Shop;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ShopController extends Controller
{
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content)
	{
		return $content
			->header('门店管理')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content)
	{
		return $content
			->header('门店管理')
			->description(' ')
			->body($this->detail($id));
	}

	public function edit($id, Content $content)
	{
		return $content
			->header('门店管理')
			->description(' ')
			->body($this->form()->edit($id));
	}

	public function update($id)
	{
		$this->form()->ignore(['prov_id', 'city_id'])->update($id);
	}

	public function create(Content $content)
	{
		return $content
			->header('门店管理')
			->description(' ')
			->body($this->form());
	}

	public function store()
	{
		$this->form()->ignore(['prov_id', 'city_id'])->store();
	}

	public function destroy($id)
	{
		if (Shop::findOrFail($id)) {
			return response()->json([
				'status'  => false,
				'message' => '暂不可删除'
			]);
		}

		if ($this->form()->destroy($id)) {
			$data = [
				'status'  => true,
				'message' => trans('admin.delete_succeeded'),
			];
		} else {
			$data = [
				'status'  => false,
				'message' => trans('admin.delete_failed'),
			];
		}

		return response()->json($data);
	}

	protected function grid()
	{
		$grid = new Grid(new Shop);

		$grid->disableFilter();

		$grid->id('ID')->sortable();
		$grid->prov_name('省');
		$grid->genre('门店类型')->using(Shop::GENRES);
//		$grid->genre('门店类型')->editable('select', Shop::GENRES);
		$grid->name('门店名称');
		$grid->tel('门店电话');
		$grid->created_at('创建时间');

		return $grid;
	}

	protected function detail($id)
	{
		$shop = Shop::findOrFail($id);

		$show = new Show($shop);

		$show->genre('门店类型')->using(Shop::GENRES);
		$show->name('门店名称');
		$show->tel('门店电话');
		$show->field('cover', '门店照片')->image();
		$show->field('charter', '营业执照')->image();
		$show->field('prov_name', '省');
		$show->field('street', '地址');
		$show->created_at('创建时间');
		$show->updated_at('更新时间');

		return $show;
	}

	protected function form()
	{
		$form = new Form(new Shop);

		$genres    = Shop::GENRES;
		$provinces = Area::options();

		$form->radio('genre', '门店类型')->options($genres)->rules('required');
		$form->text('name', '门店名称')->rules('required|string|max:16');
		$form->text('tel', '门店电话');
		$form->image('cover', '门店照片')->uniqueName()->rules('required');
		$form->image('charter', '营业执照')->uniqueName()->rules('required');
		$form->select('prov_id', '省')->setWidth(2)->options($provinces)->default(510000)->rules('required');
		$form->text('street', '街道地址')->rules('required|string|max:255');

		return $form;
	}
}
